package com.poisonh.unifiedauth.dialog;

import android.app.DialogFragment;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.poisonh.unifiedauth.R;
import com.poisonh.unifiedauth.utils.ToastUtils;
import com.poisonh.unifiedauth.widget.IndicatorWheelView;

import java.util.Arrays;

/**
 * 学历选择对话框
 * Created by PoisonH on 2016/7/6.
 */
public class EducationDialogFragment extends BaseDialogFragment implements View.OnClickListener
{
    private final String[] EDUCATION = new String[]{"专科", "本科", "硕士", "博士"};
    private TextView mTvCancel;
    private TextView mTvSure;
    private String mStrEdu="本科";

    @Override
    public void onStart()
    {
        super.onStart();
        // 设置宽度为屏宽、靠近屏幕顶部。
        Window window = getDialog().getWindow();
        window.setBackgroundDrawable(new ColorDrawable(0xff000000));
        WindowManager.LayoutParams wlp = window.getAttributes();
        wlp.gravity = Gravity.BOTTOM;
        wlp.width = WindowManager.LayoutParams.MATCH_PARENT;
        wlp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(wlp);

        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        window.setLayout(dm.widthPixels, getDialog().getWindow().getAttributes().height);
    }

//    @Override
//    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
//    {
//        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
//        View view = inflater.inflate(R.layout.layout_dialog_chooseedu, null);
//        return view;
//    }

    @Override
    protected View loadView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        return inflater.inflate(R.layout.layout_dialog_chooseedu, null);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState)
    {
        super.onViewCreated(view, savedInstanceState);
        mTvCancel = (TextView) view.findViewById(R.id.tv_cancel);
        mTvSure = (TextView) view.findViewById(R.id.tv_sure);
        mTvSure.setOnClickListener(this);
        mTvCancel.setOnClickListener(this);
        IndicatorWheelView wv = (IndicatorWheelView) view.findViewById(R.id.iwv_chooseedudialog);
        wv.setOffset(1);
        wv.setItems(Arrays.asList(EDUCATION));
        wv.setSeletion(1);
        wv.setOnWheelViewListener(new IndicatorWheelView.OnWheelViewListener()
        {
            @Override
            public void onSelected(int selectedIndex, String item)
            {
                mStrEdu = item;
            }
        });
    }

    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.tv_cancel:
                this.dismiss();
                break;
            case R.id.tv_sure:
                ChooseEduListener listener = (ChooseEduListener) getActivity();
                listener.onChooseEduListener(mStrEdu);
                this.dismiss();
                break;
        }
    }

    public interface ChooseEduListener
    {
        void onChooseEduListener(String edu);
    }
}
